<template>
  <div v-if="fileItems.length>0" class="file-list-container">
    <div v-for="file in fileItems" :key="file.fileUuid" class="file-item">
      <div v-if="file.fileType===1" class="file-info-container" @click="handleViewImage(file.fileUuid)">
        <div class="file-icon">
          <img :src="global.getFileView(file.fileUuid)" style="width: 2.9em;">
        </div>
        <div class="file-info">
          <div class="file-name">{{ file.originalFileName }}.{{ file.fileExt }}</div>
          <div class="file-size">{{ file.fileSize | formatFileSize }}</div>
        </div>
      </div>
      <fileViewer v-else :type="file.fileType" :url="file.fileUuid" :title="file.originalFileName">
        <div class="file-info-container">
          <div class="file-icon">
            <FileTypeSvgIcon :type="file.fileType" />
          </div>
          <div class="file-info">
            <div class="file-name">{{ file.originalFileName }}.{{ file.fileExt }}</div>
            <div class="file-size">{{ file.fileSize | formatFileSize }}</div>
          </div>
        </div>
      </fileViewer>
      <div v-if="remove" class="remove-icon">
        <i class="el-icon-close" @click="removeFile(file.fileUuid)" />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'FileItem',
  props: {
    files: {
      type: Array,
      default() {
        return []
      }
    },
    remove: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fileItems: []
    }
  },
  watch: {
    files: function(val) {
      this.fetchFile()
    }
  },
  created() {
    this.fetchFile()
  },
  methods: {
    fetchFile() {
      if (this.files.length > 0) {
        if (this.files[0].length !== 0) {
          this.$store.dispatch('listFileResourcesByUUID', { uuids: this.files.join(',') }).then((res) => {
            this.fileItems = res
          })
        } else {
          this.fileItems = []
        }
      } else {
        this.fileItems = []
      }
    },
    removeFile(uuid) {
      this.$emit('remove', uuid)
    },
    handleViewImage(fileUuid) {
      window.open(this.$router.resolve({ path: `/fileView/image/${fileUuid}` }).href)
    }
  }
}
</script>

<style lang="scss" scoped>
.file-list-container{
  margin-top: 15px;
  overflow: auto;
  display: flex;
  flex-wrap: wrap;
  .file-item{
    width: 300px;
    margin: 0px 12px 15px 0px;
    border-radius: 4px;
    background: #f1f4f7;
    border: 1px solid #e3e3e3;
    border-radius: 4px;
    height: 70px;
    display: flex;
    flex-direction: row;
    .file-info-container{
      display: flex;
      flex-direction: row;
      cursor: pointer;
      flex: 1;
      .file-icon{
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0 13px;
        .svg-icon{
          width: 2.9em;
          height: 2.9em;
        }
      }
      .file-info{
        display: flex;
        flex-direction: column;
        justify-content: center;
        font-size: 14px;
        width: 220px;
        cursor: pointer;
        .file-name{
          color: #404040;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
        }
        .file-size{
          margin-top: 5px;
          color: #A6A6A6;
        }
      }
    }
    .remove-icon{
      text-align: right;
      padding: 4px;
      i{
        cursor: pointer;
      }
    }
  }
}
</style>
